

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/blog/img/fluid.png">
  <link rel="icon" href="/blog/images/base.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Airfe">
  <meta name="keywords" content="">
  
    <meta name="description" content="本文介绍客户端存储。">
<meta property="og:type" content="article">
<meta property="og:title" content="客户端存储">
<meta property="og:url" content="https://airfe.gitee.io/blog/2023/11/19/%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%AD%98%E5%82%A8/index.html">
<meta property="og:site_name" content="Airfe的博客">
<meta property="og:description" content="本文介绍客户端存储。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%AD%98%E5%82%A8.png">
<meta property="article:published_time" content="2023-11-19T00:00:00.000Z">
<meta property="article:modified_time" content="2024-02-26T12:24:19.743Z">
<meta property="article:author" content="Airfe">
<meta property="article:tag" content="cookie">
<meta property="article:tag" content="storage">
<meta name="twitter:card" content="summary_large_image">
<meta name="twitter:image" content="https://markdown-1307246189.cos.ap-chongqing.myqcloud.com/pageImage/%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%AD%98%E5%82%A8.png">
  
  
  
    <meta name="referrer" content="no-referrer" />
  
  <title>客户端存储 - Airfe的博客</title>

  <link  rel="stylesheet" href="https://lib.baomitu.com/twitter-bootstrap/4.6.1/css/bootstrap.min.css" />



  <link  rel="stylesheet" href="https://lib.baomitu.com/github-markdown-css/4.0.0/github-markdown.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/hint.css/2.7.0/hint.min.css" />

  <link  rel="stylesheet" href="https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.css" />



<!-- 主题依赖的图标库，不要自行修改 -->
<!-- Do not modify the link that theme dependent icons -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_hj8rtnfg7um.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/blog/css/main.css" />


  <link id="highlight-css" rel="stylesheet" href="/blog/css/highlight.css" />
  
    <link id="highlight-css-dark" rel="stylesheet" href="/blog/css/highlight-dark.css" />
  




  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    Fluid.ctx = Object.assign({}, Fluid.ctx)
    var CONFIG = {"hostname":"airfe.gitee.io","root":"/blog/","version":"1.9.4","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false,"scope":[]},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"left","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"code_language":{"enable":true,"default":"TEXT"},"copy_btn":true,"image_caption":{"enable":true},"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"placement":"right","headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":true,"follow_dnt":true,"baidu":"699a5d71ac7179b23874a59b0666a6f6","google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","app_key":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com","path":"window.location.pathname","ignore_local":false}},"search_path":"/blog/local-search.xml"};

    if (CONFIG.web_analytics.follow_dnt) {
      var dntVal = navigator.doNotTrack || window.doNotTrack || navigator.msDoNotTrack;
      Fluid.ctx.dnt = dntVal && (dntVal.startsWith('1') || dntVal.startsWith('yes') || dntVal.startsWith('on'));
    }
  </script>
  <script  src="/blog/js/utils.js" ></script>
  <script  src="/blog/js/color-schema.js" ></script>
  

  
    <!-- Baidu Analytics -->
    <script async>
      if (!Fluid.ctx.dnt) {
        var _hmt = _hmt || [];
        (function() {
          var hm = document.createElement("script");
          hm.src = "https://hm.baidu.com/hm.js?699a5d71ac7179b23874a59b0666a6f6";
          var s = document.getElementsByTagName("script")[0];
          s.parentNode.insertBefore(hm, s);
        })();
      }
    </script>
  

  

  

  

  

  

  
    
  



  
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
  

  <header>
    

<div class="header-inner" style="height: 70vh;">
  <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/blog/">
      <strong>Airfe</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/">
                <i class="iconfont icon-home-fill"></i>
                <span>首页</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/archives/">
                <i class="iconfont icon-archive-fill"></i>
                <span>归档</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/categories/">
                <i class="iconfont icon-category-fill"></i>
                <span>分类</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/tags/">
                <i class="iconfont icon-tags-fill"></i>
                <span>标签</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/about/">
                <i class="iconfont icon-user-fill"></i>
                <span>关于</span>
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/blog/links/">
                <i class="iconfont icon-link-fill"></i>
                <span>友链</span>
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              <i class="iconfont icon-search"></i>
            </a>
          </li>
          
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">
              <i class="iconfont icon-dark" id="color-toggle-icon"></i>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

  

<div id="banner" class="banner" parallax=true
     style="background: url('/blog/images/bg.jpg') no-repeat center center; background-size: cover;">
  <div class="full-bg-img">
    <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
      <div class="banner-text text-center fade-in-up">
        <div class="h2">
          
            <span id="subtitle" data-typed-text="客户端存储"></span>
          
        </div>

        
          
  <div class="mt-3">
    
    
      <span class="post-meta">
        <i class="iconfont icon-date-fill" aria-hidden="true"></i>
        <time datetime="2023-11-19 08:00" pubdate>
          2023年11月19日
        </time>
      </span>
    
  </div>

  <div class="mt-1">
    
      <span class="post-meta mr-2">
        <i class="iconfont icon-chart"></i>
        
          7.2k 字
        
      </span>
    

    
      <span class="post-meta mr-2">
        <i class="iconfont icon-clock-fill"></i>
        
        
        
          60 分钟
        
      </span>
    

    
    
  </div>


        
      </div>

      
    </div>
  </div>
</div>

</div>

  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="side-col d-none d-lg-block col-lg-2">
      

    </div>

    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">客户端存储</h1>
            
            
              <div class="markdown-body">
                
                <p>cookie 是在客户端存储数据的一个选项。</p>
<h1 id="cookie"><a href="#cookie" class="headerlink" title="cookie"></a>cookie</h1><p>cookie最初用于在客户端存储会话信息。是一个规范，这个规范要求<strong>服务器在响应HTTP请求时，发送Set-Cookie HTTP头部包含会话信息</strong>，在之后的<strong>请求中通过HTTP头部cookie再将他们发回服务器</strong>。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bash">HTTP/1.1 200 OK <br>Content-<span class="hljs-built_in">type</span>: text/html <br>Set-Cookie: name=value <br>Other-header: other-header-value<br></code></pre></td></tr></table></figure>

<p>这个 HTTP 响应会设置一个名为”name”，值为”value”的 cookie。名和值在发送时都会经过 URL编码。浏览器会存储这些会话信息，并在之后的每个请求中都会通过 HTTP 头部 cookie 再将它们发回服务器.</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs bash">GET /index.jsl HTTP/1.1 <br>Cookie: name=value <br>Other-header: other-header-value<br></code></pre></td></tr></table></figure>

<h2 id="限制"><a href="#限制" class="headerlink" title="限制"></a>限制</h2><p><strong>cookie 是与特定域绑定的</strong>。设置 cookie 后，它会与请求一起发送到创建它的域。</p>
<p>这个限制能保证cookie 中存储的信息只对被认可的接收者开放，不被其他域访问。</p>
<p>因为 cookie 存储在客户端机器上，所以为保证它不会被恶意利用，浏览器会施加限制。同时，cookie也不会占用太多磁盘空间。</p>
<p>通常需要遵循以下大致限制，就可以在浏览器中顺畅使用cookie</p>
<ul>
<li>不超过 300 个 cookie</li>
<li>每个 cookie 不超过 4096 字节</li>
<li>每个域不超过 20 个 cookie</li>
<li>每个域不超过 81 920 字节</li>
</ul>
<p>每个域能设置的 cookie 总数也是受限的，但不同浏览器的限制不同。如果 cookie 总数超过了单个域的上限，浏览器就会删除之前设置的 cookie。</p>
<p>浏览器也会限制 cookie 的大小。大多数浏览器对 cookie 的限制是不超过 4096 字节（每个域），上下可以有一个字节的误差。</p>
<h2 id="cookie-的构成"><a href="#cookie-的构成" class="headerlink" title="cookie 的构成"></a>cookie 的构成</h2><p>cookie 在浏览器中是由以下参数构成的。</p>
<ul>
<li><p>名称：唯一标识cookie的名称，不区分大小写。不过最好实践中区分大小写。cookie 名必须经过 URL 编码。</p>
</li>
<li><p>值：存储在 cookie 里的字符串值。这个值必须经过 URL 编码。</p>
</li>
<li><p>域：cookie 有效的域。发送到这个域的所有请求都会包含对应的 cookie。这个值可能包含子域（如<a target="_blank" rel="noopener" href="http://www.wrox.com),也可以不包含(如.wrox.com/">www.wrox.com），也可以不包含（如.wrox.com</a> 表示对 wrox.com 的所有子域都有效）。如果不明确设置，则默认为设置 cookie 的域。</p>
</li>
<li><p>路径：请求 URL 中包含这个路径才会把 cookie 发送到服务器。例如，可以指定 cookie 只能由<a target="_blank" rel="noopener" href="http://www.wrox.com/books/%E8%AE%BF%E9%97%AE%EF%BC%8C%E5%9B%A0%E6%AD%A4%E8%AE%BF%E9%97%AE">http://www.wrox.com/books/访问，因此访问</a> <a target="_blank" rel="noopener" href="http://www.wrox.com/%E4%B8%8B%E7%9A%84%E9%A1%B5%E9%9D%A2%E5%B0%B1%E4%B8%8D%E4%BC%9A%E5%8F%91%E9%80%81">http://www.wrox.com/下的页面就不会发送</a> cookie，即使请求的是同一个域。</p>
</li>
<li><p>过期时间：何时删除 cookie 的时间戳（什么时间之后就不发送到服务器）。把过期时间设置为过去的时间会立即删除 cookie。</p>
<blockquote>
<p>默认情况下，浏览器会话结束之后会删除所有cookie。如果设置了过期时间，则按过期时间为准。</p>
</blockquote>
</li>
<li><p>安全标志：设置之后，只在使用 SSL 安全连接的情况下才会把 cookie 发送到服务器。例如，请求 <a target="_blank" rel="noopener" href="https://www.wrox.com/">https://www.wrox.com</a> 会发送 cookie，而请求 <a target="_blank" rel="noopener" href="http://www.wrox.com/">http://www.wrox.com</a> 则不会。</p>
</li>
</ul>
<p>这些参数在 Set-Cookie 头部中使用分号加空格隔开，比如：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs bash">HTTP/1.1 200 OK <br>Content-<span class="hljs-built_in">type</span>: text/html <br>Set-Cookie: name=value; expires=Mon, 22-Jan-07 07:10:24 GMT; domain=.wrox.com; secure<br>Other-header: other-header-value<br></code></pre></td></tr></table></figure>

<p>这个头部设置一个名为”name”的 cookie，这个 cookie 在 2007 年 1 月 22 日 7:10:24 过期，对<a target="_blank" rel="noopener" href="http://www.wrox.com/">www.wrox.com</a> 及其他 wrox.com 的子域（如 p2p.wrox.com）有效。因为设置了 secure 标志，这个 cookie 只能在 SSL 连接上发送。</p>
<blockquote>
<p>域、路径等标志用于告诉浏览器什么情况下应该在请求中包含 cookie。这些参数并不会随请求发送给服务器，实际发送的只有 cookie 的名&#x2F;值对。</p>
</blockquote>
<h2 id="JavaScript-中的-cookie"><a href="#JavaScript-中的-cookie" class="headerlink" title="JavaScript 中的 cookie"></a>JavaScript 中的 cookie</h2><p>因为在 JavaScript 中读写 cookie 不是很直观，所以可以通过辅助函数来简化相应的操作。与 cookie相关的基本操作有读、写和删除。这些在 CookieUtil 对象中表示如下：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 设置 cookie </span><br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>); <br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;book&quot;</span>, <span class="hljs-string">&quot;Professional JavaScript&quot;</span>); <br><span class="hljs-comment">// 读取 cookie </span><br><span class="hljs-title function_">alert</span>(<span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">&quot;name&quot;</span>)); <span class="hljs-comment">// &quot;Nicholas&quot; </span><br><span class="hljs-title function_">alert</span>(<span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">&quot;book&quot;</span>)); <span class="hljs-comment">// &quot;Professional JavaScript&quot; </span><br><span class="hljs-comment">// 删除 cookie </span><br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">unset</span>(<span class="hljs-string">&quot;name&quot;</span>); <br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">unset</span>(<span class="hljs-string">&quot;book&quot;</span>); <br><span class="hljs-comment">// 设置有路径、域和过期时间的 cookie </span><br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>, <span class="hljs-string">&quot;/books/projs/&quot;</span>, <span class="hljs-string">&quot;www.wrox.com&quot;</span>, <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(<span class="hljs-string">&quot;January 1, 2010&quot;</span>)); <br><span class="hljs-comment">// 删除刚刚设置的 cookie </span><br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">unset</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;/books/projs/&quot;</span>, <span class="hljs-string">&quot;www.wrox.com&quot;</span>); <br><span class="hljs-comment">// 设置安全 cookie </span><br><span class="hljs-title class_">CookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">null</span>, <span class="hljs-literal">true</span>); <br></code></pre></td></tr></table></figure>

<p>这些方法通过处理解析和 cookie 字符串构建，简化了使用 cookie 存储数据的操作。</p>
<h2 id="子-cookie"><a href="#子-cookie" class="headerlink" title="子 cookie"></a>子 cookie</h2><p>为绕过浏览器对每个域 cookie 数的限制，有些开发者提出了子 cookie 的概念。子 cookie本质上是使用 cookie 的值在单个 cookie 中存储多个名&#x2F;值对。</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs bash">name=name1=value1&amp;name2=value2&amp;name3=value3&amp;name4=value4&amp;name5=value5<br></code></pre></td></tr></table></figure>

<p>解析和序列化子 cookie 的方式不一样，要取得某个子 cookie，就需要先取得 cookie，然后在解码值之前找到子 cookie。</p>
<p>取得子 cookie 有两个方法：get()和 getAll()。</p>
<ul>
<li>get()：取得一个子 cookie 的值。接收参数（2）：cookie 的名称和子 cookie 的名称。</li>
<li>getAll()：取得所有子 cookie，并以对象形式返回。对象的属性是子 cookie 的名称，值是子 cookie 的值。接收参数（1）：cookie 的名称。</li>
</ul>
<blockquote>
<p>get()方法也是先调用了getAll()取得所有子 cookie，返回要取得的子 cookie（如果不存在则返回 null）。</p>
</blockquote>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 假设 document.cookie=data=name=Nicholas&amp;book=Professional%20JavaScript</span><br><span class="hljs-comment">// 取得所有子 cookie</span><br><span class="hljs-keyword">let</span> data = <span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">getAll</span>(<span class="hljs-string">&quot;data&quot;</span>);<br><span class="hljs-title function_">alert</span>(data.<span class="hljs-property">name</span>); <span class="hljs-comment">// &quot;Nicholas&quot; </span><br><span class="hljs-title function_">alert</span>(data.<span class="hljs-property">book</span>); <span class="hljs-comment">// &quot;Professional JavaScript&quot;</span><br><br><span class="hljs-comment">// 取得个别子 cookie </span><br><span class="hljs-title function_">alert</span>(<span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>)); <span class="hljs-comment">// &quot;Nicholas&quot; </span><br><span class="hljs-title function_">alert</span>(<span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">get</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;book&quot;</span>)); <span class="hljs-comment">// &quot;Professional JavaScript&quot;</span><br></code></pre></td></tr></table></figure>

<p>要写入子 cookie，可以使用另外两个方法：set()和 setAll()。</p>
<ul>
<li>set()：接收7个参数，cookie 的名称、子 cookie 的名称、子 cookie 的值、可选的 Date 对象用于设置 cookie 的过期时间、可选的 cookie 路径、可选的 cookie 域和可选的布尔值 secure 标志。</li>
<li>setAll()：接收6 个参数，cookie 的名称、包含所有子 cookie 的对象，然后是 set()方法中使用的 4 个可选参数。</li>
</ul>
<p>可以像下面这样使用这些方法：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 假设 document.cookie=data=name=Nicholas&amp;book=Professional%20JavaScript </span><br><span class="hljs-comment">// 设置两个子 cookie </span><br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>); <br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;book&quot;</span>, <span class="hljs-string">&quot;Professional JavaScript&quot;</span>); <br><span class="hljs-comment">// 设置所有子 cookie 并传入过期时间</span><br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">setAll</span>(<span class="hljs-string">&quot;data&quot;</span>, &#123; <span class="hljs-attr">name</span>: <span class="hljs-string">&quot;Nicholas&quot;</span>, <span class="hljs-attr">book</span>: <span class="hljs-string">&quot;Professional JavaScript&quot;</span> &#125;, <br> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(<span class="hljs-string">&quot;January 1, 2010&quot;</span>)); <br></code></pre></td></tr></table></figure>

<p>常规 cookie 可以通过直接设置过期时间为某个过去的时间删除，但删除子 cookie 没有这么简单。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 修改&quot;name&quot;的值并修改整个 cookie 的过期时间</span><br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">set</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Michael&quot;</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(<span class="hljs-string">&quot;February 1, 2010&quot;</span>));<br></code></pre></td></tr></table></figure>

<p>为了删除子 cookie，需要先取得所有子 cookie，把要删除的那个删掉，然后再把剩下的子 cookie 设置回去。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-keyword">class</span> <span class="hljs-title class_">SubCookieUtil</span> &#123; <br>    <span class="hljs-comment">// 省略之前的代码</span><br>    <span class="hljs-keyword">static</span> <span class="hljs-title function_">unset</span>(<span class="hljs-params">name, subName, path, domain, secure</span>) &#123; <br>        <span class="hljs-keyword">let</span> subcookies = <span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">getAll</span>(name); <br>        <span class="hljs-keyword">if</span> (subcookies)&#123; <br>            <span class="hljs-keyword">delete</span> subcookies[subName]; <span class="hljs-comment">// 删除</span><br>            <span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">setAll</span>(name, subcookies, <span class="hljs-literal">null</span>, path, domain, secure); <br>        &#125; <br>    &#125; <br>    <span class="hljs-keyword">static</span> <span class="hljs-title function_">unsetAll</span>(<span class="hljs-params">name, path, domain, secure</span>) &#123; <br>        <span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">setAll</span>(name, <span class="hljs-literal">null</span>, <span class="hljs-keyword">new</span> <span class="hljs-title class_">Date</span>(<span class="hljs-number">0</span>), path, domain, secure); <br>    &#125; <br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 只删除&quot;name&quot;子 cookie </span><br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">unset</span>(<span class="hljs-string">&quot;data&quot;</span>, <span class="hljs-string">&quot;name&quot;</span>);<br><span class="hljs-comment">// 删除整个 cookie </span><br><span class="hljs-title class_">SubCookieUtil</span>.<span class="hljs-title function_">unsetAll</span>(<span class="hljs-string">&quot;data&quot;</span>);<br></code></pre></td></tr></table></figure>

<h2 id="使用-cookie-的注意事项"><a href="#使用-cookie-的注意事项" class="headerlink" title="使用 cookie 的注意事项"></a>使用 cookie 的注意事项</h2><p>尽可能只通过 cookie 保存必要信息，以避免性能问题。</p>
<p>因为所有 cookie 都会作为请求头部由浏览器发送给服务器，所以在 cookie 中保存大量信息可能会影响特定域浏览器请求的性能。保存的 cookie 越大，请求完成的时间就越长。即使浏览器对 cookie 大小有限制，最好还是尽可能只通过 cookie 保存必要信息，以避免性能问题。</p>
<blockquote>
<p>不要在 cookie 中存储重要或敏感的信息。cookie 数据不是保存在安全的环境中，因此任何人都可能获得。</p>
</blockquote>
<p>对 cookie 的限制及其特性决定了 cookie 并不是存储大量数据的理想方式。因此，其他客户端存储技术出现了。</p>
<h1 id="Web-Storage"><a href="#Web-Storage" class="headerlink" title="Web Storage"></a>Web Storage</h1><p>Web Storage 的目的是解决 客户端存储不需要频繁发送回服务器的数据时使用 cookie 的问题。</p>
<p>Web Storage 规范最新的版本是第 2 版，这一版规范主要有两个目标：</p>
<ul>
<li>提供在 cookie 之外的存储会话数据的途径；</li>
<li>提供跨会话持久化存储大量数据的机制。</li>
</ul>
<p>Web Storage 的第 2 版定义了两个对象：localStorage 和 sessionStorage。<strong>localStorage 是永久存储机制，sessionStorage 是跨会话的存储机制。</strong></p>
<p>这两种浏览器存储 API 提供了在浏览器中不受页面刷新影响而存储数据的两种方式。</p>
<h2 id="Storage-类型"><a href="#Storage-类型" class="headerlink" title="Storage 类型"></a><strong>Storage</strong> 类型</h2><p>Storage 类型用于保存名&#x2F;值对数据，直至存储空间上限（由浏览器决定）。特殊的有以下方法：</p>
<ul>
<li>clear()：删除所有值；不在 Firefox 中实现。</li>
<li>getItem(<em>name</em>)：取得给定 <em>name</em> 的值。</li>
<li>key(<em>index</em>)：取得给定数值位置的名称。</li>
<li>removeItem(<em>name</em>)：删除给定 <em>name</em> 的名&#x2F;值对</li>
<li>setItem(<em>name</em>, <em>value</em>)：设置给定 <em>name</em> 的值。</li>
</ul>
<p>通过 length 属性可以确定 Storage 对象中保存了多少名&#x2F;值对。</p>
<blockquote>
<p>Storage 类型只能存储字符串。非字符串数据在存储之前会自动转换为字符串。注意，这种转换不能在获取数据时撤销。</p>
</blockquote>
<h2 id="sessionStorage-对象"><a href="#sessionStorage-对象" class="headerlink" title="sessionStorage 对象"></a><strong>sessionStorage</strong> 对象</h2><p>sessionStorage 对象只存储会话数据，这意味着数据只会存储到浏览器关闭。</p>
<p>存储在sessionStorage 对象中的数据只能由最初存储数据的页面使用，在多页应用程序中的用处有限。(在一个页面中使用<code>sessionStorage</code>存储的数据，不能被同一域下的其他页面所访问。每个页面都有自己独立的<code>sessionStorage</code>存储空间。)</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用方法存储数据</span><br>sessionStorage.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>); <br><span class="hljs-comment">// 使用属性存储数据</span><br>sessionStorage.<span class="hljs-property">book</span> = <span class="hljs-string">&quot;Professional JavaScript&quot;</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用方法取得数据</span><br><span class="hljs-keyword">let</span> name = sessionStorage.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&quot;name&quot;</span>); <br><span class="hljs-comment">// 使用属性取得数据</span><br><span class="hljs-keyword">let</span> book = sessionStorage.<span class="hljs-property">book</span>;<br></code></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用 delete 删除值</span><br><span class="hljs-keyword">delete</span> sessionStorage.<span class="hljs-property">name</span>; <br><span class="hljs-comment">// 使用方法删除值</span><br>sessionStorage.<span class="hljs-title function_">removeItem</span>(<span class="hljs-string">&quot;book&quot;</span>);<br></code></pre></td></tr></table></figure>

<p>sessionStorage 对象应该主要用于存储只在会话期间有效的小块数据。如果需要跨会话持久存储数据，可以使用 localStorage。</p>
<h2 id="localStorage-对象"><a href="#localStorage-对象" class="headerlink" title="localStorage 对象"></a><strong>localStorage</strong> 对象</h2><p>localStorage 对象，作为在客户端持久存储数据的机制。</p>
<p>要访问同一个 localStorage 对象，页面必须来自同一个域（子域不可以）、在相同的端口上使用相同的协议。</p>
<p>因 为 localStorage 是 Storage 的实例，所以可以像使用 sessionStorage 一样使用localStorage。比如下面这几个例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-comment">// 使用方法存储数据</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">setItem</span>(<span class="hljs-string">&quot;name&quot;</span>, <span class="hljs-string">&quot;Nicholas&quot;</span>);<br><span class="hljs-comment">// 使用属性存储数据</span><br><span class="hljs-variable language_">localStorage</span>.<span class="hljs-property">book</span> = <span class="hljs-string">&quot;Professional JavaScript&quot;</span>;<br><span class="hljs-comment">// 使用方法取得数据</span><br><span class="hljs-keyword">let</span> name = <span class="hljs-variable language_">localStorage</span>.<span class="hljs-title function_">getItem</span>(<span class="hljs-string">&quot;name&quot;</span>);<br><span class="hljs-comment">// 使用属性取得数据</span><br><span class="hljs-keyword">let</span> book = <span class="hljs-variable language_">localStorage</span>.<span class="hljs-property">book</span>;<br></code></pre></td></tr></table></figure>

<p>两种存储方法的区别在于，存储在 localStorage 中的数据会保留到通过 JavaScript 删除或者用户清除浏览器缓存。localStorage 数据不受页面刷新影响，也不会因关闭窗口、标签页或重新启动浏览器而丢失。</p>
<h2 id="存储事件"><a href="#存储事件" class="headerlink" title="存储事件"></a>存储事件</h2><p>对于 sessionStorage 和 localStorage 上的任何更改都会触发 storage 事件，但 storage 事件不会区分这两者。</p>
<p>这个事件对象有如下4个属性：</p>
<ul>
<li>domain：存储变化对应的域。</li>
<li>key：被设置或删除的键。</li>
<li>newValue：键被设置的新值，若键被删除则为 null。</li>
<li>oldValue：键变化之前的值。</li>
</ul>
<p>可以使用如下代码监听 storage 事件：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs js"><span class="hljs-variable language_">window</span>.<span class="hljs-title function_">addEventListener</span>(<span class="hljs-string">&quot;storage&quot;</span>, <br> <span class="hljs-function">(<span class="hljs-params">event</span>) =&gt;</span> <span class="hljs-title function_">alert</span>(<span class="hljs-string">&#x27;Storage changed for $&#123;event.domain&#125;&#x27;</span>));<br></code></pre></td></tr></table></figure>
































                
              </div>
            
            <hr/>
            <div>
              <div class="post-metas my-3">
  
    <div class="post-meta mr-3 d-flex align-items-center">
      <i class="iconfont icon-category"></i>
      

<span class="category-chains">
  
  
    
      <span class="category-chain">
        
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/" class="category-chain-item">前端技术栈</a>
  
  
    <span>></span>
    
  <a href="/blog/categories/%E5%89%8D%E7%AB%AF%E6%8A%80%E6%9C%AF%E6%A0%88/js/" class="category-chain-item">js</a>
  
  

  

      </span>
    
  
</span>

    </div>
  
  
    <div class="post-meta">
      <i class="iconfont icon-tags"></i>
      
        <a href="/blog/tags/cookie/">#cookie</a>
      
        <a href="/blog/tags/storage/">#storage</a>
      
    </div>
  
</div>


              
  

  <div class="license-box my-3">
    <div class="license-title">
      <div>客户端存储</div>
      <div>https://airfe.gitee.io/blog/2023/11/19/客户端存储/</div>
    </div>
    <div class="license-meta">
      
        <div class="license-meta-item">
          <div>作者</div>
          <div>Airfe</div>
        </div>
      
      
        <div class="license-meta-item license-meta-date">
          <div>发布于</div>
          <div>2023年11月19日</div>
        </div>
      
      
      
        <div class="license-meta-item">
          <div>许可协议</div>
          <div>
            
              
              
                <a target="_blank" href="https://creativecommons.org/licenses/by/4.0/">
                  <span class="hint--top hint--rounded" aria-label="BY - 署名">
                    <i class="iconfont icon-by"></i>
                  </span>
                </a>
              
            
          </div>
        </div>
      
    </div>
    <div class="license-icon iconfont"></div>
  </div>



              
                <div class="post-prevnext my-3">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/blog/2024/03/21/%E6%96%AD%E7%82%B9%E7%BB%AD%E5%AD%98/" title="断点续存">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">断点续存</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/blog/2023/11/16/JSON/" title="JSON">
                        <span class="hidden-mobile">JSON</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
  
  
    <article id="comments" lazyload>
      
  <div id="valine"></div>
  <script type="text/javascript">
    Fluid.utils.loadComments('#valine', function() {
      Fluid.utils.createScript('https://lib.baomitu.com/valine/1.5.1/Valine.min.js', function() {
        var options = Object.assign(
          {"appId":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appKey":"P5fzMTGSJIoTaGvmgzsUHeNZ","path":"window.location.pathname","placeholder":"发表你的看法吧~","avatar":"retro","meta":["nick","mail","link"],"requiredFields":[],"pageSize":10,"lang":"zh-CN","highlight":false,"recordIP":false,"serverURLs":"","emojiCDN":null,"emojiMaps":null,"enableQQ":false,"appid":"RW68dMkEpeIOJJiBr1lPkhOS-gzGzoHsz","appkey":"P5fzMTGSJIoTaGvmgzsUHeNZ","server_url":"https://rw68dmke.lc-cn-n1-shared.com"},
          {
            el: "#valine",
            path: window.location.pathname
          }
        )
        new Valine(options);
        Fluid.utils.waitElementVisible('#valine .vcontent', () => {
          var imgSelector = '#valine .vcontent img:not(.vemoji)';
          Fluid.plugins.imageCaption(imgSelector);
          Fluid.plugins.fancyBox(imgSelector);
        })
      });
    });
  </script>
  <noscript>Please enable JavaScript to view the comments</noscript>


    </article>
  


          </article>
        </div>
      </div>
    </div>

    <div class="side-col d-none d-lg-block col-lg-2">
      
  <aside class="sidebar" style="margin-left: -1rem">
    <div id="toc">
  <p class="toc-header">
    <i class="iconfont icon-list"></i>
    <span>目录</span>
  </p>
  <div class="toc-body" id="toc-body"></div>
</div>



  </aside>


    </div>
  </div>
</div>





  



  



  



  



  







    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v" for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>

    

    
  </main>

  <footer>
    <div class="footer-inner">
  
    <div class="footer-content">
        <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> <div style="font-size: 0.85rem"> <span id="timeDate">载入天数</span> <span id="times">载入时分秒</span> <script src="js/duration.js"></script> </div> 
    </div>
  
  
    <div class="statistics">
  
  

  
    
      <span id="leancloud-site-pv-container" style="display: none">
        总访问量 
        <span id="leancloud-site-pv"></span>
         次
      </span>
    
    
      <span id="leancloud-site-uv-container" style="display: none">
        总访客数 
        <span id="leancloud-site-uv"></span>
         人
      </span>
    
    

  
</div>

  
  
    <!-- 备案信息 ICP for China -->
    <div class="beian">
  <span>
    <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
      蜀ICP备2023008943号
    </a>
  </span>
  
    
      <span>
        <a
          href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=https://beian.miit.gov.cn/"
          rel="nofollow noopener"
          class="beian-police"
          target="_blank"
        >
          
            <span style="visibility: hidden; width: 0">|</span>
            <img src="" srcset="/blog/img/loading.gif" lazyload data-original="/blog/img/police_beian.png" alt="police-icon"/>
          
          <span>京公网安备12345678号</span>
        </a>
      </span>
    
  
</div>

  
  
</div>

  </footer>

  <!-- Scripts -->
  
  <script  src="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://lib.baomitu.com/nprogress/0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://lib.baomitu.com/jquery/3.6.0/jquery.min.js" ></script>
<script  src="https://lib.baomitu.com/twitter-bootstrap/4.6.1/js/bootstrap.min.js" ></script>
<script  src="/blog/js/events.js" ></script>
<script  src="/blog/js/plugins.js" ></script>


  <script  src="https://lib.baomitu.com/typed.js/2.0.12/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var subtitle = document.getElementById('subtitle');
      if (!subtitle || !typing) {
        return;
      }
      var text = subtitle.getAttribute('data-typed-text');
      
        typing(text);
      
    })(window, document);
  </script>




  
    <script  src="/blog/js/img-lazyload.js" ></script>
  




  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/tocbot/4.18.2/tocbot.min.js', function() {
    var toc = jQuery('#toc');
    if (toc.length === 0 || !window.tocbot) { return; }
    var boardCtn = jQuery('#board-ctn');
    var boardTop = boardCtn.offset().top;

    window.tocbot.init(Object.assign({
      tocSelector     : '#toc-body',
      contentSelector : '.markdown-body',
      linkClass       : 'tocbot-link',
      activeLinkClass : 'tocbot-active-link',
      listClass       : 'tocbot-list',
      isCollapsedClass: 'tocbot-is-collapsed',
      collapsibleClass: 'tocbot-is-collapsible',
      scrollSmooth    : true,
      includeTitleTags: true,
      headingsOffset  : -boardTop,
    }, CONFIG.toc));
    if (toc.find('.toc-list-item').length > 0) {
      toc.css('visibility', 'visible');
    }

    Fluid.events.registerRefreshCallback(function() {
      if ('tocbot' in window) {
        tocbot.refresh();
        var toc = jQuery('#toc');
        if (toc.length === 0 || !tocbot) {
          return;
        }
        if (toc.find('.toc-list-item').length > 0) {
          toc.css('visibility', 'visible');
        }
      }
    });
  });
</script>


  <script src=https://lib.baomitu.com/clipboard.js/2.0.11/clipboard.min.js></script>

  <script>Fluid.plugins.codeWidget();</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/anchor-js/4.3.1/anchor.min.js', function() {
    window.anchors.options = {
      placement: CONFIG.anchorjs.placement,
      visible  : CONFIG.anchorjs.visible
    };
    if (CONFIG.anchorjs.icon) {
      window.anchors.options.icon = CONFIG.anchorjs.icon;
    }
    var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
    var res = [];
    for (var item of el) {
      res.push('.markdown-body > ' + item.trim());
    }
    if (CONFIG.anchorjs.placement === 'left') {
      window.anchors.options.class = 'anchorjs-link-left';
    }
    window.anchors.add(res.join(', '));

    Fluid.events.registerRefreshCallback(function() {
      if ('anchors' in window) {
        anchors.removeAll();
        var el = (CONFIG.anchorjs.element || 'h1,h2,h3,h4,h5,h6').split(',');
        var res = [];
        for (var item of el) {
          res.push('.markdown-body > ' + item.trim());
        }
        if (CONFIG.anchorjs.placement === 'left') {
          anchors.options.class = 'anchorjs-link-left';
        }
        anchors.add(res.join(', '));
      }
    });
  });
</script>


  
<script>
  Fluid.utils.createScript('https://lib.baomitu.com/fancybox/3.5.7/jquery.fancybox.min.js', function() {
    Fluid.plugins.fancyBox();
  });
</script>


  <script>Fluid.plugins.imageCaption();</script>

  <script defer src="/blog/js/leancloud.js" ></script>

  <script  src="/blog/js/local-search.js" ></script>





<!-- 主题的启动项，将它保持在最底部 -->
<!-- the boot of the theme, keep it at the bottom -->
<script  src="/blog/js/boot.js" ></script>


  

  <noscript>
    <div class="noscript-warning">博客在允许 JavaScript 运行的环境下浏览效果更佳</div>
  </noscript>

        <style>
            [bg-lazy] {
                background-image: none !important;
                background-color: #eee !important;
            }
        </style>
        <script>
            window.imageLazyLoadSetting = {
                isSPA: false,
                preloadRatio: 1,
                processImages: null,
            };
        </script><script>window.addEventListener("load",function(){var t=/\.(gif|jpg|jpeg|tiff|png)$/i,r=/^data:image\/[a-z]+;base64,/;Array.prototype.slice.call(document.querySelectorAll("img[data-original]")).forEach(function(a){var e=a.parentNode;"A"===e.tagName&&(e.href.match(t)||e.href.match(r))&&(e.href=a.dataset.original)})});</script><script>!function(r){r.imageLazyLoadSetting.processImages=t;var e=r.imageLazyLoadSetting.isSPA,n=r.imageLazyLoadSetting.preloadRatio||1,c=a();function a(){var t=Array.prototype.slice.call(document.querySelectorAll("img[data-original]")),e=Array.prototype.slice.call(document.querySelectorAll("[bg-lazy]"));return t.concat(e)}function t(){e&&(c=a());for(var t,o=0;o<c.length;o++)0<=(t=(t=c[o]).getBoundingClientRect()).bottom&&0<=t.left&&t.top<=(r.innerHeight*n||document.documentElement.clientHeight*n)&&function(){var t,e,n,a,i=c[o];e=function(){c=c.filter(function(t){return i!==t}),r.imageLazyLoadSetting.onImageLoaded&&r.imageLazyLoadSetting.onImageLoaded(i)},(t=i).hasAttribute("bg-lazy")?(t.removeAttribute("bg-lazy"),e&&e()):(n=new Image,a=t.getAttribute("data-original"),n.onload=function(){t.src=a,t.removeAttribute("data-original"),e&&e()},t.src!==a&&(n.src=a))}()}function i(){clearTimeout(t.tId),t.tId=setTimeout(t,500)}t(),document.addEventListener("scroll",i),r.addEventListener("resize",i),r.addEventListener("orientationchange",i)}(this);</script></body>
</html>
